using System;
using UnityEngine;
using Unity.VisualScripting;

namespace LS.VisualScripting
{
    public class SetDataNode : DataNodeUnit
    {
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput DataNodePath { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Data { get; set; }
        
        protected override void Definition()
        {
            base.Definition();
            DataNodePath = ValueInput<string>(nameof(DataNodePath), string.Empty);
            Data = ValueInput<object>(nameof(Data), null);
            Requirement(DataNodePath, Input);
            Requirement(Data, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            string path = flow.GetValue<string>(DataNodePath);
            object newValue = flow.GetValue<object>(Data);
            
            if(string.IsNullOrEmpty(path))
                throw new ArgumentNullException("DataNodePath");
            if (newValue == null)
                throw new ArgumentNullException("Data");
            Target?.SetData(path, newValue);
            return base.In(flow);
        }
    }
}